<template>
    <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="检验单编号" prop="rrqcCode">
              <el-input v-model="form.rrqcCode" placeholder="请输入检验单编号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检验单名称" prop="rrqcName">
              <el-input v-model="form.rrqcName" placeholder="请输入检验单名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
<!--          <el-col :span="8">
            <el-form-item label="检验类型" prop="rrqcType">
              <el-select v-model="form.rrqcType" placeholder="请选择检验类型" disabled="disabled">
                <el-option
                  v-for="dict in dict.type.mes_ipqc_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>-->
          <el-col :span="8">
            <el-form-item label="返修单编号" prop="sourceDocCode">
              <el-input v-model="form.sourceDocCode" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">

          </el-col>
        </el-row>
          <el-collapse v-model="activeNames">
            <el-collapse-item name="1">
              <template slot="title">
                更多信息<i class="header-icon el-icon-info"></i>
              </template>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="产品编码" prop="itemCode">
                    <el-input v-model="form.itemCode" placeholder="请选择任务" readonly />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="产品名称" prop="itemName">
                    <el-input v-model="form.itemName" placeholder="请选择任务" readonly/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="单位" prop="unitOfMeasure">
                    <el-input v-model="form.unitOfMeasure" placeholder="请选择任务" readonly/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="规格型号" prop="specification">
                    <el-input v-model="form.specification" type="textarea" placeholder="请选择任务" readonly/>
                  </el-form-item>
                </el-col>
              </el-row>
<!--              <el-row>
                <el-col :span="12">
                  <el-form-item label="工序编码" prop="processCode">
                    <el-input v-model="form.processCode" placeholder="请选择任务" readonly/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="工序名称" prop="processName">
                    <el-input v-model="form.processName" placeholder="请选择任务" readonly/>
                  </el-form-item>
                </el-col>
              </el-row>-->


            </el-collapse-item>
          </el-collapse>

<!--        <el-row>

            <el-col :span="12">
            <el-form-item label="任务编号" prop="taskCode">
                <el-input v-model="form.taskCode" placeholder="请输入任务编号" readonly/>
            </el-form-item>
            </el-col>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="taskName">
              <el-input v-model="form.taskName" placeholder="请输入任务名称" readonly/>
            </el-form-item>
          </el-col>
        </el-row>-->

          <el-row style="margin-top: 10px;">
            <el-col :span="8">
              <el-form-item label="检测数量" prop="quantityCheck">
                <el-input-number
                  :min="0"
                  v-model="form.quantityCheck"
                  placeholder="请输入检测数量"
                  readonly
                  style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="不合格数" prop="quantityUnqualified">
                <el-input-number
                  :min="0"
                  v-model="form.quantityUnqualified"
                  placeholder="请输入不合格数量"
                  style="width: 100%"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="合格品数量" prop="quantityQualified">
                <el-input-number
                  :min="0"
                  v-model="form.quantityQualified"
                  placeholder="请输入合格品数量"
                  style="width: 100%"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="致命缺陷数量" prop="crQuantity">
                <el-input v-model="form.crQuantity" placeholder="请输入致命缺陷数量" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="严重缺陷数量" prop="majQuantity">
                <el-input v-model="form.majQuantity" placeholder="请输入严重缺陷数量" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="轻微缺陷数量" prop="minQuantity">
                <el-input v-model="form.minQuantity" placeholder="请输入轻微缺陷数量" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="检测日期" prop="inspectDate">
                <el-date-picker clearable
                                v-model="form.inspectDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择检测日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="检测结果" prop="checkResult">
                <el-select v-model="form.checkResult" placeholder="请选择检验结果">
                  <el-option
                    v-for="dict in dict.type.mes_qc_result"
                    :key="dict.value"
                    v-if="dict.value!='EXEMPT'"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="检测人员" prop="inspector">
                <el-input v-model="form.inspector" placeholder="请输入检测人员" readonly>
                  <el-button slot="append" @click="handleSelectUser" icon="el-icon-search"></el-button>
                </el-input>
                <UserSelect ref="userSelect" @onSelected="onUserSelected" ></UserSelect>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
              </el-form-item>
            </el-col>
          </el-row>
      </el-form>
      <el-divider v-if="form.rrqcId !=null" content-position="center">检测项</el-divider>
      <el-card shadow="always" v-if="form.rrqcId !=null" class="box-card">
          <Rrqcline ref=line :rrqcId="form.rrqcId" :optType="optType" @onHandleReset="handleReset"></Rrqcline>
      </el-card>
      <el-divider v-if="form.rrqcId !=null" content-position="center">检测过程</el-divider>
      <el-card shadow="always" v-if="form.rrqcId != null" class="box-card">
        <FileUpload
          :limit="5"
          :fileSize="100"
          :fileType="fileType"
          :value="fileList"
          :optType="optType"
          @onUploaded="handleUploaded"
          @onRemoved="handleRemoved">
        </FileUpload>
      </el-card>
      <el-form label-width="100px">
        <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
            <el-button type="primary" @click="cancel" v-if="optType =='view' || form.status !='PREPARE' ">返回</el-button>
            <el-button type="primary" @click="submitForm" v-if="form.status =='PREPARE' && optType !='view' ">保 存</el-button>
            <el-button type="success" @click="handleFinish" v-if="form.status =='PREPARE' && optType !='view'  && form.rrqcId !=null">完成</el-button>
            <el-button @click="cancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>
<script>
import { listRrqc, getRrqc, delRrqc, addRrqc, updateRrqc,getRrqcInfoBySourceDocId } from "@/api/mes/qc/rrqc";
import { genCode } from "@/api/system/autocode/rule"
import WorkorderSelect from "@/components/workorderSelect/single.vue"
import WorkstationSelect from "@/components/workstationSelect/simpletableSingle.vue"
import Rrqcline from "../rrqc/line.vue";
import UserSelect from "@/components/userSelect/single.vue";
import { getUserProfile } from "@/api/system/user";

export default {
  name: "rqcAdd",
  components: { WorkorderSelect, WorkstationSelect, Rrqcline, UserSelect },
  dicts: ['mes_ipqc_type','mes_qc_result','mes_order_status'],
  props: {
    workOrderId: null,
    workOrderCode: null,
    workOrderName: null,
    workstationId: null,
    workstationCode: null,
    workstationName: null,
    taskId: null,
    taskCode: null,
    taskName: null,
    itemId: null,
    itemCode: null,
    itemName: null,
    quantityCheck: null
  },
  data() {
    return {
      activeNames: '1',
      autoGenFlag: false,
      optType: 'add',
      // 表单参数
      form: {},
      // 上传文件类型
      fileType: ["pdf","doc", "xls","jpg","png"],
      // 上传文件列表
      fileList: [],
      // 表单校验
      rules: {
        rrqcCode: [
          { required: true, message: "请输入或生产检验单编号", trigger: "blur" }
        ],
        rrqcType: [
          { required: true, message: "请选择检验类型", trigger: "change" }
        ],
        workorderCode: [
          { required: true, message: "请选择生产工单", trigger: "blur" }
        ],
        workstationCode: [
          { required: true, message: "请选择工作站", trigger: "blur" }
        ],
        quantityCheck: [
          { required: true, message: "检测数量不能为空", trigger: "blur" }
        ],
        quantityUnqualified: [
          { required: true, message: "不合格品数量不能为空", trigger: "blur" }
        ],
        quantityQualified: [
          { required: true, message: "合格品数量不能为空", trigger: "blur" }
        ],
      },
      // 当前登录用户
      user: null,
    }
  },
  created() {
    this.reset();

    if(this.$route.params.sourceDocId) {
      getRrqcInfoBySourceDocId(this.$route.params.sourceDocId).then(response => {
        if (response.data) {
          this.form = response.data;
          if(!this.form.rrqcCode){
            genCode('RQC_CODE').then(response =>{
              this.form.rrqcCode = response;
            });
          }
          this.form.quantityCheck = this.$route.params.quantityUncheck;
          this.form.status = 'PREPARE';
        } else {
          this.reset();
          genCode('RQC_CODE').then(response =>{
            this.form.rrqcCode = response;
          });
        }
      })
    }
  },
  methods: {
    // 取消按钮
    cancel() {
        const obj = { path: "/mes/qc/pendinginspect" };
        this.$tab.closeOpenPage(obj);
    },
    handleReset(){
      getRrqc(this.form.rrqcId).then(response => {
        this.form.crQuantity = response.data.crQuantity;
        this.form.majQuantity = response.data.majQuantity;
        this.form.minQuantity = response.data.minQuantity;
        this.form.attr2 = response.data.attr2;
        response.data.attr2 === null ? this.fileList = [] : this.fileList = response.data.attr2.split(',');
      });
    },
    // 表单重置
    reset() {
      this.form = {
        rrqcId: null,
        rrqcCode: null,
        rrqcName: null,
        rrqcType: 'RECHECK',
        templateId: null,
        sourceDocId: this.$route.params.sourceDocId,
        sourceDocType: this.$route.params.sourceDocType,
        sourceDocCode: this.$route.params.sourceDocCode,
        sourceLineId: this.$route.params.sourceLineId,
        workorderId: this.$route.params.workOrderId,
        workorderCode: this.$route.params.workOrderCode,
        workorderName: this.$route.params.workOrderName,
        taskId: this.$route.params.taskId,
        taskCode: this.$route.params.taskCode,
        taskName: this.$route.params.taskName,
        workstationId: this.$route.params.workstationId,
        workstationCode: this.$route.params.workstationCode,
        workstationName: this.$route.params.workstationName,
        processId: null,
        processCode: null,
        processName: null,
        itemId: this.$route.params.itemId,
        itemCode: this.$route.params.itemCode,
        itemName: this.$route.params.itemName,
        specification: this.$route.params.specification,
        unitOfMeasure: this.$route.params.unitOfMeasure,
        quantityCheck: this.$route.params.quantityUncheck,
        quantityUnqualified: null,
        quantityQualified: null,
        crRate: null,
        majRate: null,
        minRate: null,
        crQuantity: null,
        majQuantity: null,
        minQuantity: null,
        checkResult: null,
        inspectDate: new Date(),
        inspector: null,
        status: "PREPARE",
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.autoGenFlag = false;
      this.resetForm("form");
      this.getUser();
      this.fileList = [];
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rrqcId != null) {
            updateRrqc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
            });
          } else {
            addRrqc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.form.rrqcId = response.data; //将返回的ID保存
            });
          }
        }
      });
    },
    //点击完成
    handleFinish(){
      let that = this;
      if(this.form.checkResult == null){
        this.$modal.msgError("请选择检测结果！");
        return;
      }
      let num = parseInt(that.form.quantityCheck);
      let he = that.form.quantityQualified + that.form.quantityUnqualified
      if (num !== he) {
        this.$modal.msgError("合格品数量与不合格品数量之和不等于检测数量！")
        return;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$modal.confirm('是否完成检验单编制？【完成后将不能更改】').then(function(){
            that.form.status = 'FINISHED';
            that.submitForm();
            that.cancel();
          });
        }
      });
    },

    handleSelectUser(){
      this.$refs.userSelect.showFlag = true;
    },
    // 用户选择弹出框
    onUserSelected(obj){
      if(obj != undefined && obj != null){
        this.form.inspector = obj.nickName;
      }
    },
    // 获取登录用户
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
        this.form.inspector = this.user.nickName;
      });
    },
    handleUploaded(data) {
      this.form.attr2 = data;
    },
    handleRemoved(data) {
      this.form.attr2 = data;
    },
  }
}
</script>
